//
//  OtherOrderList.swift
//  LeGou
//
//  Created by lxm on 2022/8/5.
//

import Foundation
import UIKit

class OtherOrderList: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {
    //MARK: --------------------属性--------------------
    var listData = [MineModels.OtherOrder]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    
    // 1会员商城订单 2话费充值 3悬赏任务 4签到任务 5美团订单
    // status 订单状态（0：已生成(已付款)、1已结算、2已失效）
    var status : ProfitState = .all
    //MARK: --------------------UI控件--------------------
    let tableView = UITableView().then { t in
        t.separatorStyle = .none
        t.tableFooterView = UIView()
    }
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        tableView.mj_header?.beginRefreshing()
    }
    
    //MARK: --------------------UI--------------------
    func setUI() {
        title = ""
        setUpTableView()
        view.backgroundColor = "#F5F5F5".zm.hex()
    }
    
    func setUpTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = "#F5F5F5".zm.hex()

        tableView.zm.register(nibName: "PorfitListCell", reuserId: "PorfitListCell")
        
        view.addSubview(tableView)
        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无数据"
        emptyImg = "nodata"
    }
    
    func addConstraints() {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    func loadList() {
        var pa = ["limit" : pageSize, "page" : page]
        if status != .all {
            pa["status"] = status.rawValue
        }
        ZMUtil.listRequest(type: MineModels.OtherOrder.self, api: MineAPI.thirdOrder(pa)) { resp in
            self.listRequest(resp)
        }
    }

    //MARK: --------------------delegate--------------------
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PorfitListCell") as! PorfitListCell
        let data = listData[indexPath.row]
        if data.image.contains("http") {
            cell.img.zm.setImage(urlStr: data.image)
        } else {
            cell.img.zm.setImage(urlStr: "https:" + data.image)
        }
        if data.orderType == .mt {
            cell.tbIcon.image = UIImage.init(named: "third_mt")
        } else if data.orderType == .elm {
            cell.tbIcon.image = UIImage.init(named: "third_elm")
        } else if data.orderType == .tb || data.orderType == .newBuy {
            cell.tbIcon.image = UIImage.init(named: "home_taobao")
        } else if data.orderType == .jd {
            cell.tbIcon.image = UIImage.init(named: "home_jd")
        } else if data.orderType == .pdd {
            cell.tbIcon.image = UIImage.init(named: "home_pdd")
        } else if data.orderType == .power {
            cell.tbIcon.image = UIImage.init(named: "home_df")
            cell.img.image = UIImage(named: "power_5")
        } else {
            cell.tbIcon.image = UIImage.init(named: "icon_512")
        }
        cell.name.text = data.orderTitle
        cell.order_new_icon.isHidden = data.orderType != .newBuy
        if data.status == .payed {
            cell.state.text = "已付款"
            cell.state.textColor = "#333333".zm.hex()
            cell.tips.text = "订单号：\(data.orderId)"
        } else if data.status == .invalid {
            cell.state.text = "已失效"
            cell.state.textColor = "#999999".zm.hex()
            cell.tips.text = "订单号：\(data.orderId)"
        } else if data.status == .over {
            cell.state.text = "已结算"
            cell.state.textColor = "#999999".zm.hex()
            let text = "结算时间：\(data.settleTime)\n".zm.attributedStr(attrs: [.foregroundColor : "#333333".zm.hex(), .font : UIFont.systemFont(ofSize: 14, weight: .regular)]).zm.append(str: "订单号：\(data.orderId)", attri: ["c" : "#666666", "f" : "12"])
            let pa = NSMutableParagraphStyle()
            pa.lineSpacing = 8
            text.addAttributes([.paragraphStyle : pa], range: NSRange(location: 0, length: text.length))
            cell.tips.attributedText = text
        }
        
        cell.setStyle(s: data.status == .invalid ? 1 : 0)
        cell.partnerIcon.isHidden = data.type == 0
        cell.orderNo.text = data.orderCreateTime
        cell.refundMoney.text = data.income.zm.toString(keep: 4)
        
        cell.refundBack.isHidden = data.income == 0
        return cell
    }
    
}

